// pages/notification/notification.js
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notificationList:[],
    limitData:[],
    page:1,
    count:5
  },
  getNotificationList(){
    let that = this;
    this.setData({
      page:1
    })
    wx.request({
      url: app.globalData.serviceUrl+'/notification/getNotificationList',
      method:"POST",
      success:(res)=>{
        that.setData({
          limitData:res.data.data.filter(item=>item.userID==app.globalData.userInfo.id&&item.userType==app.globalData.storage.userType).reverse(),
          notificationList:res.data.data.filter(item=>item.userID==app.globalData.userInfo.id&&item.userType==app.globalData.storage.userType).reverse().slice(0,this.data.page*this.data.count)
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  readNotify(){
    if(this.data.notificationList.filter(item=>item.read==0).length!==0){
      wx.request({
        url: app.globalData.serviceUrl+'/notification/readNotification',
        method:"POST",
        header:{
          "content-type":"application/x-www-form-urlencoded"
        },
        data:{
          userID:app.globalData.userInfo.id,
          userType:app.globalData.storage.userType
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getNotificationList();
    this.readNotify()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getNotificationList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.notificationList.length===this.data.limitData.length){
      return
    }
    let toast = Toast.loading("加载中")
    let page = this.data.page+=1
    this.setData({
      page:page,
      notificationList:this.data.limitData.slice(0,page*this.data.count)
    })
    this.readNotify();
    toast.clear();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})